package com.sykj.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sykj.bean.DianZhan;
import com.sykj.service.DianZhanService;
import com.sykj.util.redis.RedisUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

/**
 * @program: BeautyClub
 * @description:
 * @author: qiaoli
 * @create: 2020-08-13 22:42
 */
public class DianzhanTimeJob extends QuartzJobBean {
    private final static Logger log = LoggerFactory.getLogger(DianzhanTimeJob.class);
    @Autowired
    DianZhanService zhanService;
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//获取JobDetail中关联的数据
        String msg = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("msg");


        try {
            //先获取这段时间的浏览数
            Map<Object,Object> viewCountItem= RedisUtil.hmget(RedisKeys.ARTICLE_VIEWCOUNT_KEY);
            //然后删除redis里这段时间的浏览数
            RedisUtil.del(RedisKeys.ARTICLE_VIEWCOUNT_KEY);
            if(!viewCountItem.isEmpty()){
                for(Object item :viewCountItem.keySet()){
                    String articleKey=item.toString();//viewcount_1
                    String[]  kv=articleKey.split("_");
                    String articleId=kv[1].toString();
                    Integer viewCount=(Integer) viewCountItem.get(articleKey);
                    UpdateWrapper<DianZhan> updateWrapper=new UpdateWrapper<>();

                    QueryWrapper queryWrapper=new QueryWrapper();
                    queryWrapper.eq("project",articleId);
                    DianZhan one = zhanService.getOne(queryWrapper);
                    if(one==null){
                        DianZhan dianZhan =new DianZhan();
                        Timestamp timeStamp = new Timestamp(new Date().getTime());
                        dianZhan.setCreateTime(timeStamp);
                        dianZhan.setProject(articleId);
                        dianZhan.setHits(1);
                        zhanService.save(dianZhan);
                    }

                    Timestamp timeStamp = new Timestamp(new Date().getTime());
                    int hits = one.getHits();
                    hits=hits+viewCount;
                    updateWrapper.set("create_time",timeStamp);
                    updateWrapper.set("hits",hits);
                    updateWrapper.eq("project",articleId+"");
                    zhanService.update(updateWrapper);
                    //更新到数据库

                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        log.info(msg);
        System.err.println(msg);
    }
}
